local s = require('utils.misc')
local t = require('utils.table')
local b = require('utils.buf')
local strings = require('plenary.strings')

local g = vim.g
local fnamemodify = vim.fn.fnamemodify

return {
    "jonahfang/cd-project.nvim",
    keys = {
        { "<M-p>", mode = {"n","x"}, function()
            require('utils.allinit').all_init()
            vim.cmd('CdProject')
        end,desc="select projects"},
        { "<M-a>", mode = {"n","x"}, function()
            local last_p = vim.g.cd_project_last_project
            if not last_p then
                return
            end

            local bs = vim.g.divo_not_save_buffers
            if #bs > 0 then
                require('notify')('Modified buffer exists!')
                return
            end

            require('utils.allinit').all_init()

            last_p = fnamemodify(last_p, ":~")
            local back = "Back to " .. last_p
            vim.ui.select({ back, "Cancel" }, { prompt = "Confirm please?" }, function(choice)
                if choice == back then
                    vim.cmd('CdProjectBack')
                end
            end)
        end,desc="back to last project"},
    },
    config = function()
        --/home/fzx/work/nvim (config file)
        require("cd-project").setup({
            projects_config_filepath = vim.fs.normalize(
                vim.fn.stdpath("config") .. "/../cd-project.nvim.json"
            ),
            project_dir_pattern = { ".git", ".gitignore", "Cargo.toml", "package.json" },
            before_switch_project = function()
                local bs = vim.g.divo_not_save_buffers
                if #bs == 0 then
                    return true
                end
                require('notify')('Modified buffer exists!')
                return false
            end,
            after_switch_project = function(dir)
                vim.cmd("Neotree close")
                vim.cmd([[Lazy reload fff.nvim]])

                b.close_all()
                require('helper.quick_recently').show_short_mru()

                g.divo_git_branch_done = 0

                require('helper.tide').close()
                require('helper.tide').setup()

                require('notify')('Switched to ' .. dir)
            end,
            format_project_path = function(path,name)
                local dirs = s.str_split("/",fnamemodify(path, ":~"))
                local new_item = ''
                local n = 1
                t.foreach(dirs,function(v)
                    if n > 2 then
                        new_item = new_item .. '/' .. v
                    end
                    n = n + 1
                end)
                local w = strings.strdisplaywidth(name)+strings.strdisplaywidth(new_item)
                local s = string.rep(' ',75-w)
                return name .. s .. new_item
            end,
            project_filter = function(dir)
                local cwd = vim.fn.getcwd()
                if dir == cwd then
                    return 0
                end
                local main_dir = s.str_split("/",fnamemodify(cwd, ":~"))[2]
                local p_main_dir = s.str_split("/",fnamemodify(dir, ":~"))[2]
                if main_dir == p_main_dir then
                    return 1
                else
                    return 0
                end
            end
        })
    end,
}
--EOP
